var Options = function()
{
    this.STORAGE_KEY = 'csPreferences';
    this.SEPARATOR = String.fromCharCode(182);

    this.array = new Array();

    this.get = function(index)  { return this.array[index]; }
    this.count = function()     { return this.array.length; }

    this.toString = function()
    {
        var str = '';
        for (var i = 0; i < this.array.length; ++i)
        {
            str += this.array[i].name + this.SEPARATOR + this.array[i].url + this.SEPARATOR;
        }
        return str.substring(0, str.length - 1);
    }

    this.loadFromStorage = function()
    {
        this.array = new Array();
        var oprionsString = localStorage[this.STORAGE_KEY];
        if (typeof(oprionsString) != 'undefined' && oprionsString.length > 0)
        {
            var strs = oprionsString.split(this.SEPARATOR);
            for (var i = 0; i < strs.length; i += 2)
            {
                if (strs[i] != '')
                    this.array.push({ name: strs[i], url: strs[i+1] });
            }
        }
        else
        {
            this.array[0] = {name: 'Google', url: 'http://www.google.com/search?q=%s' };
            this.array[1] = {name: 'Wikipedia', url: 'http://en.wikipedia.org/w/index.php?search=%s' };
            this.array[2] = {name: 'Navigate', url: '%s' };
        }
        localStorage[this.STORAGE_KEY] = this.toString();
    }

    this.loadFromStorage();

    this.changeOrder = function(index1, index2)
    {
        var tempOption = this.array[index1];
        this.array[index1] = this.array[index2];
        this.array[index2] = tempOption;
        localStorage[this.STORAGE_KEY] = this.toString();
    }

    this.save = function(index, option)
    {
        this.array[index] = option;
        localStorage[this.STORAGE_KEY] = this.toString();
    }

    this.remove = function(index)
    {
        this.array.splice(index, 1);
        localStorage[this.STORAGE_KEY] = this.toString();
    }
}
